<template>
  <el-button
      type="primary"
      aria-label="添加角色"
      @click="show()"
  >添加角色
  </el-button>

  <el-dialog
      title="添加角色"
      v-model="addFormVisible"
  >
    <el-form
        :rules="addRoleRules"
        :model="addRl">
      <el-form-item label="角色名称"
                    prop="roleName">
        <el-input
            v-model="addRl.roleName"
            autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="角色描述" prop="roleDesc">
        <el-input
            v-model="addRl.roleDesc"
            autocomplete="off"></el-input>
      </el-form-item>
    </el-form>
    <template #footer>
    <span class="dialog-footer">
      <el-button @click="addFormVisible = false">取 消</el-button>
      <el-button type="primary" @click="addRole">确 定</el-button>
    </span>
    </template>
  </el-dialog>
</template>

<script>
import axios from "axios";

export default {
  name: "AddRoleBtn",
  emits:['showAddDialog','update'],
  data() {
    return {
      addRl: {
        roleName: '',
        roleDesc: '',
      },
      addRoleRules: {
        roleName: [
          {required: true, message: '请输入角色名', trigger: 'blur'},
          {min: 2, max: 16, message: '请输入2-16个字符', trigger: 'blur'}
        ],
        roleDesc: [
          {required: true, message: '请输入角色描述', trigger: 'blur'},
          {min: 2, max: 16, message: '请输入2-16个字符', trigger: 'blur'}
        ]
      },
      addFormVisible: false
    }
  },
  methods: {
    async addRole() {
      this.$message.info("addRole click")
      const {data: res} = await axios.post('roles', this.addRl)

      if (res.meta.status !== 201) {
        return this.$message.error(res.meta.msg)
      }

      this.$message.success(res.meta.msg)
      this.addFormVisible = false
      this.$emit('update')
    },
    show() {
      this.addFormVisible = true
    }
  }
}
</script>

<style scoped>

</style>